package com.yuwei.sign

import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.util.Log
import com.alibaba.android.arouter.facade.annotation.Route
import com.amap.api.location.AMapLocationClient
import com.amap.api.location.AMapLocationClientOption
import com.amap.api.location.AMapLocationListener
import com.amap.api.maps.AMap
import com.amap.api.maps.MapView
import com.amap.api.maps.model.MyLocationStyle
import com.yuwei.base.BaseActivity
import com.yuwei.base.path.RouterPath
import com.yuwei.sign.databinding.ActivitySignBinding
import com.yuwei.sign.viewmodel.SignViewModel
import dagger.hilt.android.AndroidEntryPoint
import java.text.SimpleDateFormat
import java.util.*

@AndroidEntryPoint
@Route(path = RouterPath.MODULE_SIGN_ACTIVITY)
class SignActivity : BaseActivity<ActivitySignBinding,SignViewModel>() {
    private lateinit var handler: Handler
    private lateinit var runnable: Runnable
    //声明AMapLocationClient类对象
    //声明AMapLocationClient类对象
    lateinit var mLocationClient: AMapLocationClient
    //声明定位回调监听器
    //声明定位回调监听器
    val mLocationListener = AMapLocationListener {
        if (it.errorCode == 0) {
            //可在其中解析amapLocation获取相应内容。
            Log.d("TagA66","定位成功${it.address}")
        }else {
            //定位失败时，可通过ErrCode（错误码）信息来确定失败的原因，errInfo是错误信息，详见错误码表。
            Log.e("TagA66","定位失败, 错误码:"
                    + it.errorCode + ", 错误信息:"
                    + it.locationDetail
            )
        }
    }
    lateinit var mLocationOption: AMapLocationClientOption
    @SuppressLint("SetTextI18n", "SimpleDateFormat")
    override fun initView() {

        mLocationClient = AMapLocationClient(applicationContext)
        //设置定位回调监听
        //设置定位回调监听
        mLocationClient.setLocationListener(mLocationListener)
        //初始化AMapLocationClientOption对象
        //初始化AMapLocationClientOption对象
        mLocationOption = AMapLocationClientOption()
        //设置是否返回地址信息（默认返回地址信息）
        mLocationOption.isNeedAddress = true
        mLocationOption.locationPurpose = AMapLocationClientOption.AMapLocationPurpose.SignIn
        mLocationClient.setLocationOption(mLocationOption)
        //设置场景模式后最好调用一次stop，再调用start以保证场景模式生效
        mLocationClient.stopLocation()
        mLocationClient.startLocation()
        val currentTime = SimpleDateFormat("HH:mm:ss", Locale.getDefault())
        val formatterTime = SimpleDateFormat("yyyy-MM-dd")
        val formatterTimeClass = SimpleDateFormat("EEEE")
        binding.timeYear.text = ""+formatterTime.format(System.currentTimeMillis())+" "+formatterTimeClass.format(System.currentTimeMillis())
        handler = Handler()
        runnable = object :Runnable{
            override fun run() {
                val formatter1 = System.currentTimeMillis()
                val tt = currentTime.format(formatter1)
                binding.timeHour.text = ""+tt
                handler.postDelayed(this,1000)
            }
        }
        handler.post(runnable)
        binding.signBack.setOnClickListener {
            finish()
        }

    }

    override fun initData() {

    }

    override fun onDestroy() {
        super.onDestroy()
        handler.removeCallbacks(runnable)
    }
}